package trabajo.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import trabajo.excepcion.DAOExcepcion;
import trabajo.modelo.Persona;
import trabajo.util.ConexionBD;

public class PersonaDAO extends BaseDAO{
	
	public Collection<Persona> lista() throws DAOExcepcion {		
		String query = "select * from persona";
		Collection<Persona> lista = new ArrayList<Persona>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);			
			rs = stmt.executeQuery();
			while (rs.next()) {
				Persona pe = new Persona();
				pe.setIdPersona(rs.getInt("idPersona"));
				
				pe.setNombres(rs.getString("nombres"));
				pe.setPaterno(rs.getString("paterno"));
				pe.setMaterno(rs.getString("materno"));
				pe.setSexo(rs.getString("sexo"));
			//lo.setTelefono(rs.getString("telefono"));				
				lista.add(pe);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}
	
	public Persona obtener(int _idPersona) throws DAOExcepcion {
		System.out.println("PersonaDAO: obtener(int _idPersona)");
		Persona persona = new Persona();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select * from local where idlocal = ?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, _idPersona);
			rs = stmt.executeQuery();
			if (rs.next()) {
				//local.setIdPersona(rs.getInt("idlocal"));
				persona.setIdPersona(rs.getInt("idpersona"));							
				persona.setNombres(rs.getString("nombres"));
				persona.setMaterno(rs.getString("materno"));
				persona.setPaterno(rs.getString("paterno"));
				persona.setSexo(rs.getString("sexo"));
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return persona;
	}
	
	
	
	//registrar una persona
	public void registar(Persona _persona) throws DAOExcepcion {
		
		System.out.println("PersonaDAO: registarPersona(Persona _persona)");
			
		ResultSet rs = null;
		Connection con = null;
		PreparedStatement stmt = null;		
			
		try {
			con = ConexionBD.obtenerConexion();
			String query;
			
			//insertando local
			query = "insert into persona (tipo, nombres, materno, paterno) VALUES (?, ?, ?, ?)";
			stmt = con.prepareStatement(query);
			
			stmt.setString(1, _persona.getIdTipo());				
			stmt.setString(2, _persona.getNombres());						
			stmt.setString(3, _persona.getPaterno());
			stmt.setString(4, _persona.getMaterno());
			//stmt.setString(5, _local.getSexo());
	//		stmt.setString(6, _local.getTelefono());
			

			int i = stmt.executeUpdate();				
				if (i != 1) {
					throw new SQLException("No se pudo insertar");
					}										
			} catch (SQLException e) {
				System.err.println(e.getMessage());
				throw new DAOExcepcion(e.getMessage());
			} finally {
				this.cerrarStatement(stmt);
				this.cerrarConexion(con);
			}
		}	

}
